﻿
#region Using Directives.

using System;
using System.Drawing;
using System.Windows.Forms;
using ActiproSoftware.SyntaxEditor;
using GDFlacTool.Common;

#endregion

namespace GDFlacTool.Forms
{
    public partial class FormGotoLine : Form
    {
        #region Fields.

        private readonly int _totalLines;
        private readonly int _currentLine;
        private int _selectedLine;
        private readonly SyntaxEditor _syntaxEditor;

        #endregion

        #region Constructors.

        public FormGotoLine(SyntaxEditor syntaxEditor)
        {
            InitializeComponent();

            _syntaxEditor = syntaxEditor;

            _totalLines = _syntaxEditor.Document.Lines.Count;

            _currentLine = _syntaxEditor.Document.Lines.IndexOf(_syntaxEditor.Caret.Offset) + 1;

            lblLineNumber.Text = string.Format("Line number (1 - {0})", _totalLines);

            txtLineNumber.Text = _currentLine.ToString();

            txtLineNumber.SelectAll();
        }

        #endregion

        #region Events.

        private void BtnOkClick(object sender, EventArgs e)
        {
            int result;

            if(int.TryParse(txtLineNumber.Text, out result))
            {
                if(result > _totalLines)
                {
                    _selectedLine = _totalLines;
                }
                else if(result < 1)
                {
                    _selectedLine = _currentLine;
                }
                else
                {
                    _selectedLine = result;
                }
            }
            else
            {
                _selectedLine = _currentLine;
            }

            _syntaxEditor.SelectedView.GoToLine(_selectedLine - 1);
        }

        private void TxtLineNumberKeyPress(object sender, KeyPressEventArgs e)
        {
            ttpTextBox.RemoveAll();

            if(Functions.ValidateKeyChar(e.KeyChar))
            {
                var p = new Point(txtLineNumber.Location.X + 20, txtLineNumber.Location.Y - 90);
                ttpTextBox.Show("You can only type a number here.", txtLineNumber, p, 3000);
                e.Handled = true;
            }
        }

        #endregion
    }
}
